package com.baizhi.service.impl;

import com.baizhi.entity.Category;
import com.baizhi.mapper.CategoryMapper;
import com.baizhi.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author huiJie
 * @since 2021-08-31
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> showChild(Integer parentId, List<Category> categories) {
        return categories.stream().filter(c->c.getParentId()==parentId.longValue()).peek((c)->{
            c.setChilds(showChild(c.getCategoryId().intValue(),categories));
        }).collect(Collectors.toList());
    }

    @Override
    public List<Category> showCategoryByLevel() {
        List<Category> categories1 = baseMapper.selectList(null);
        QueryWrapper<Category> wrapper = new QueryWrapper<Category>();
        wrapper.eq("parent_id","0");
        List<Category> categories = baseMapper.selectList(wrapper);
        List<Category> list = categories.stream().peek((c) -> {
            c.setChilds(showChild(c.getCategoryId().intValue(),categories1));
        }).collect(Collectors.toList());
        return list;
    }
}
